import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;


public class GUI extends JFrame {	
	
	public static void main(String args[]) {
		GUI gui = new GUI();
	}
	
	/****************************************
	 * Attributes
	 ****************************************/
	private static final Component LatestPostP = null;
	private JTabbedPane jTabbedPane1;
	private JPanel LastestPostP;
	public JButton save_msg;
	public JTextField jTextField1;
	
	public JRadioButtonMenuItem messager;
	public JRadioButtonMenuItem twitter;
	public ButtonGroup curN;

	public JRadioButton jRadioButton4;
	public JRadioButton jRadioButton3;
	public JRadioButton jRadioButton2;
	public JRadioButton jRadioButton1;
	
	private JPanel DefaultMsg;
	public JTextArea textArea;
	public JScrollPane jScrollPane1;
	private JLabel jLabel1;
	
	public JPanel Location;
	public JTextField id_location;
	public JButton bSaveId;
	public JTextArea posTA;
	
	private JPanel Connect;
	public JTextField TportName;
	public JButton bSavePort;

	private SocWear socWear;
	
	private int selectedMsg = -1; // define the selected msg in changing default msg
	public String inputDefaultMsg = "";
	
	private String[] defaultMsg;
	public URL url;
	public ImageIcon image;
	public JLabel imgL;
	
	public LocationManager lm;
	
	
	/****************************************
	 * Constructor
	 ****************************************/
	// call initGUI() and init the style of tabbed page
	public GUI() {
		super();
		
		socWear = new SocWear(this, "D:/msg.txt");
		lm = new LocationManager("D:/AppServ/www/test.txt");
		initGUI();
		setListeners();
	}

	
	/****************************************
	 * Initialize the GUI
	 ****************************************/
	private void initGUI() {
		
		defaultMsg = socWear.pm.getAllDefaultMessages();

		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

			//three tabbed pages allow users select their functions
			{
				jTabbedPane1 = new JTabbedPane();
				getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
				
				// display the latest post from twitter and messenger
				// after 'save' button is pressed, auto back to this page

				
				{
					LastestPostP = new JPanel();
					jTabbedPane1.addTab("Latest Post(s)", null, LastestPostP, null);
					jTabbedPane1.setBackground(Color.pink);
					LastestPostP.add(getTextArea());
					
					LastestPostP.add(getJTextField());
					LastestPostP.add(getSaveButton());

				}
				
				//allow users to set thier default messages
				{
					DefaultMsg = new JPanel(new GridLayout(0, 1));
					jTabbedPane1.addTab("DefaultMsg", null, DefaultMsg, null);
					DefaultMsg.add(getJRadioButton1());
					DefaultMsg.add(getJRadioButton2());
					DefaultMsg.add(getJRadioButton3());
					DefaultMsg.add(getJRadioButton4());
					DefaultMsg.add(getJTextField1());
					DefaultMsg.add(getSave_msg());
				}
				
				//allow users to change the notification pattern of fds from twitter and messenger
				{
					Location = new JPanel();
					JLabel idL = new JLabel("ID:");
					Location.add(idL);
					
					jTabbedPane1.addTab("Location", null, Location, null);
					id_location = new JTextField();
					Location.add(id_location);
					id_location.setText("Please Input the ID Here");
					
					bSaveId = new JButton();
					Location.add(bSaveId);
					bSaveId.setText("Locate");
					
					JLabel posL = new JLabel("Lat Lng:");
					Location.add(posL);
					
					posTA = new JTextArea();
					Location.add(posTA);
					posTA.setColumns(10);
					posTA.setLineWrap(true);
					posTA.setRows(1);
					posTA.setWrapStyleWord(true);
					posTA.setEditable(false);
					
					
					url = new URL("http://maps.google.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=400x400&maptype=roadmap&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false");
					image = new ImageIcon(url);
					imgL = new JLabel(" ", image, JLabel.CENTER);
					
					Location.add(imgL);
				}

				
			}
			pack();
			setSize(600, 600);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
		
		// button group of four default messages
		ButtonGroup group = new ButtonGroup();
	    group.add(jRadioButton1);
	    group.add(jRadioButton2);
	    group.add(jRadioButton3);
	    group.add(jRadioButton4);
		
	    // button group of two current network
	    curN = new ButtonGroup ();
	    curN.add(messager);
	    curN.add(twitter);
	    
	    if (selectedMsg != -1){jTextField1.setEditable(true); System.out.println("enable!");}
	    if (jTextField1.isEditable()){
	    	jTextField1.addMouseListener(new MouseListener() {
					
				@Override
				public void mouseClicked(MouseEvent arg0) {}
				@Override
				public void mouseEntered(MouseEvent arg0) {}
				@Override
				public void mouseExited(MouseEvent arg0) {}
				@Override
				public void mousePressed(MouseEvent arg0) {}
				@Override
				public void mouseReleased(MouseEvent arg0) {
						jTextField1.setText("");
				}
				
			});
	    }
	    jTextField1.setEditable(false);
	}
	
	private JTextField getJTextField() {
		if(TportName == null) {
			
			TportName = new JTextField();
			LastestPostP.add(TportName);
			TportName.setText("Please Input the Port Name Here");
	
			TportName.setBounds(40, 5, 207, 36);
			
		}
		return TportName;
	}
	
	private JButton getSaveButton(){
		if (bSavePort == null){
			// Connect button
			bSavePort = new JButton();
			bSavePort.setText("Connect");
			bSavePort.setBounds(253, 11, 95, 24);
			LastestPostP.add(bSavePort);
			
			bSavePort.addMouseListener(new MouseListener() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					socWear.connectToiCATch(TportName.getText());
					TportName.setText("Connected to " + TportName.getText());
					bSavePort.setEnabled(false);
				}
				public void mouseEntered(MouseEvent arg0) {}
				public void mouseExited(MouseEvent arg0) {}
				public void mousePressed(MouseEvent arg0) {}
				public void mouseReleased(MouseEvent arg0) {}
			});
		}
		return bSavePort;
	}
	

	/****************************************
	 * The first default message
	 ****************************************/
	private JRadioButton getJRadioButton1() {
		if(jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			
			jRadioButton1.setText("^ : " + defaultMsg[0]);
			jRadioButton1.addMouseListener(new MouseListener() {

				@Override
				public void mouseClicked(MouseEvent arg0) {}
				@Override
				public void mouseEntered(MouseEvent arg0) {}
				@Override
				public void mouseExited(MouseEvent arg0) {}
				@Override
				public void mousePressed(MouseEvent arg0) {
					if (selectedMsg == -1) {
						selectedMsg = 0;
						jTextField1.setEditable(true);
						System.out.println("radio button 1 is Pressed!");
					}
				}
				@Override
				public void mouseReleased(MouseEvent arg0) {}
			});
		}
		return jRadioButton1;
	}
	
	
	/****************************************
	 * The second default message
	 ****************************************/
	private JRadioButton getJRadioButton2() {
		if(jRadioButton2 == null) {
			jRadioButton2 = new JRadioButton();
			
			jRadioButton2.setText("> : " + defaultMsg[1]);
			jRadioButton2.addMouseListener(new MouseListener() {
				@Override
				public void mouseClicked(MouseEvent arg0) {}
				@Override
				public void mouseEntered(MouseEvent arg0) {}
				@Override
				public void mouseExited(MouseEvent arg0) {}
				@Override
				public void mousePressed(MouseEvent arg0) {
					selectedMsg = 1;
					jTextField1.setEditable(true);
					System.out.println("radio button 2 is Pressed!");
					
				}
				@Override
				public void mouseReleased(MouseEvent arg0) {}
			});
		}
		return jRadioButton2;
	}

	
	/****************************************
	 * The third default message
	 ****************************************/
	private JRadioButton getJRadioButton3() {
		if(jRadioButton3 == null) {
			jRadioButton3 = new JRadioButton();
			jRadioButton3.setText("v : " + defaultMsg[2]);
			jRadioButton3.addMouseListener(new MouseListener() {

				@Override
				public void mouseClicked(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mousePressed(MouseEvent arg0) {
					selectedMsg = 2;
					jTextField1.setEditable(true);
					System.out.println("radio button 3 is Pressed!");
					
				}

				@Override
				public void mouseReleased(MouseEvent arg0) {
					
				}
				
			});
		}
		return jRadioButton3;
	}

	
	/****************************************
	 * The fourth default message
	 ****************************************/
	private JRadioButton getJRadioButton4() {
		if(jRadioButton4 == null) {
			jRadioButton4 = new JRadioButton();
			jRadioButton4.setText("< : " + defaultMsg[3]);
			jRadioButton4.addMouseListener(new MouseListener() {

				@Override
				public void mouseClicked(MouseEvent arg0) {}
				@Override
				public void mouseEntered(MouseEvent arg0) {}
				@Override
				public void mouseExited(MouseEvent arg0) {}
				@Override
				public void mousePressed(MouseEvent arg0) {
					selectedMsg = 3;
					jTextField1.setEditable(true);
					System.out.println("radio button 4 is Pressed!");	
				}
				@Override
				public void mouseReleased(MouseEvent arg0) {}
			});
		}
		return jRadioButton4;
	}
	
	
	/****************************************
	 * Text box in change default msg, 
	 * allow users to input their updated default msg
	 ****************************************/

	private JTextField getJTextField1() {
		if(jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setText("Pleas type your msg here and press the button \"save\"");
			
		}
		return jTextField1;
	}
	
	
	/****************************************
	 * Button in change default msg
	 * Save to 
	 ****************************************/

	private JButton getSave_msg() {
		if(save_msg == null) {
			save_msg = new JButton();
			save_msg.setText("Save");
			save_msg.addMouseListener(new MouseListener() {

				@Override
				public void mouseClicked(MouseEvent arg0) {}
				@Override
				public void mouseEntered(MouseEvent arg0) {}
				@Override
				public void mouseExited(MouseEvent arg0) {}
				@Override
				public void mousePressed(MouseEvent arg0) {}
				@Override
				public void mouseReleased(MouseEvent arg0) {
					// save button is clicked, input msg is saved in inputDefaultMsg
					
					inputDefaultMsg = jTextField1.getText();
					defaultMsg[selectedMsg] = inputDefaultMsg;
					System.out.println(defaultMsg);
					switch(selectedMsg){
						case 0:
							jRadioButton1.setText("^ : "+inputDefaultMsg);
							socWear.pm.saveDefaultMessage();
							break;
						case 1:
							jRadioButton2.setText("> : "+inputDefaultMsg);
							socWear.pm.saveDefaultMessage();
							break;
						case 2:
							jRadioButton3.setText("v : "+inputDefaultMsg);
							socWear.pm.saveDefaultMessage();
							break;
						case 3:
							jRadioButton4.setText("< : "+inputDefaultMsg);
							socWear.pm.saveDefaultMessage();
							break;
					}
				}
				
			});
			
			
		}
		return save_msg;
	}

	/****************************************
	 * Text area showing latest tweets
	 ****************************************/
	public  JTextArea getTextArea(){
		textArea = new JTextArea();
		textArea.setColumns(35);
        textArea.setLineWrap(true);
        textArea.setRows(25);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        textArea.setText("From Twitter : ");
 
        jScrollPane1 = new JScrollPane(textArea);
		return textArea;
	}
	
	
	private void setListeners() {
		
		bSaveId.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {	
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				Location location = lm.getMyLocation();
				
				double lat = location.latitude;
				double lng = location.longtitude;
				
				try {
					Location.remove(imgL);
					url = new URL("http://maps.google.com/maps/api/staticmap?center=" + lat + "," + lng + "&markers=color:blue|label:S|" + lat + "," + lng + "&zoom=16&size=400x400&maptype=roadmap&sensor=false");
					image = new ImageIcon(url);
					imgL = new JLabel(" ", image, JLabel.CENTER);
					Location.add(imgL);
					
					posTA.setText(lat + "," + lng);
					
					Location.updateUI();
					
					
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
		});			
	}
}
